from fastapi import Request, APIRouter

from frame import ModelRegister, AmisJSONResponse, APIRegister

router = APIRouter(prefix="/api")


class TestModel(ModelRegister):
    base_url = "/test/"
    primary_key = "id"

    auth = True
    auth_type = "sk"

    def get_item(self, request: Request):
        id_ = request.get(self.primary_key)
        content = {"name": "mock data", "id": id_}
        return AmisJSONResponse(content=content)

    def delete_item(self, request: Request):
        id_ = request.get(self.primary_key)
        content = {"name": "mock data", "id": id_}

        return AmisJSONResponse(content=content, status_code=401, msg="失败")

    def put_item(self, request: Request):
        id_ = request.get(self.primary_key)
        content = {"name": "mock data", "id": id_}
        return AmisJSONResponse(content=content)

    def options_item(self, request: Request):
        id_ = request.get(self.primary_key)
        content = {"name": "mock data", "id": id_}
        return AmisJSONResponse(content=content)

    def get_items(self, request: Request):
        content = [{"name": f"mock data {id_}", "id": id_} for id_ in range(10)]
        return AmisJSONResponse(content=content)

    def post_item(self, request: Request):
        content = {"name": "mock data", "id": 0}
        return AmisJSONResponse(content=content)


@router.post("/login")
def post_login(request: Request):
    content = {}
    return AmisJSONResponse(content=content, headers={"token": "sk-abc"})


TestModel().register(router=router)
